﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using T.FAS.Runtime.Metadata.Entity;
using T.ServiceComponent.DAC.Service;
using T.ServiceComponent.DataAccessEx.Core;

namespace T.FAS.Runtime.Metadata.Dao
{
    internal class MetaDataPropertyDao : DaoService<MetaDataProperty>
    {
        public MetaDataPropertyDao(string db = "TFAS") : base()
        {
            this.DBName = db;
        }

        public override string IDColumnName
        {
            get
            {
                return "ID";
            }
        }

        public override string TableName
        {
            get
            {
                return "Meta_Property";
            }
        }
        public List<MetaDataPropertyHelper> GetMetaDataPropertyHelper(string metaTypeID, string tenantID)
        {
            List<IDbDataParameter> paramsList = new List<IDbDataParameter>() {
                this.DB.MakeInParam("@TenantID", TDbDataType.VarChar, tenantID),
                this.DB.MakeInParam("@MetaTypeID", TDbDataType.VarChar, metaTypeID)
            };
            string sqlCondition = "MetaTypeID=@MetaTypeID AND TenantID=@TenantID";
            string sql = string.Format("select ID,Code, Description,HelperLink,DemoCode From {0} WHERE {1} Order by SortCode", TableName, sqlCondition);
            List<MetaDataPropertyHelper> list = new List<MetaDataPropertyHelper>();
            var ds = this.DB.ExecuteDataSet(sql, paramsList.ToArray());
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow row in ds.Tables[0].Rows)
                {
                    MetaDataPropertyHelper helper = new MetaDataPropertyHelper() {
                        ID = Convert.ToString(row["ID"]),
                        Code = Convert.ToString(row["Code"]),
                        Description = Convert.ToString(row["Description"]),
                        HelperLink = Convert.ToString(row["HelperLink"]),
                        DemoCode = Convert.ToString(row["DemoCode"]),
                    };
                    list.Add(helper);
                }
            }
            return list;
        }
        public List<MetaDataProperty> GetMetaDataProperties(string metaTypeID, string tenantID)
        {
            List<IDbDataParameter> paramsList = new List<IDbDataParameter>() {
                this.DB.MakeInParam("@TenantID", TDbDataType.VarChar, tenantID),
                this.DB.MakeInParam("@MetaTypeID", TDbDataType.VarChar, metaTypeID)
            };
            string sqlCondition = "MetaTypeID=@MetaTypeID AND TenantID=@TenantID";
            string sql = string.Format("select ID,MetaTypeID,Type,Code, Name, ValueType,Description,HelperLink,DemoCode,PropertyEditor,DefaultValue,IsHide,OuterLayer,TenantID,SortCode,Creator, CreateTime, LastModifier, LastModifyTime From {0} WHERE {1} Order by SortCode", TableName, sqlCondition);
            List<MetaDataProperty> list = new List<MetaDataProperty>();
            var ds = this.DB.ExecuteDataSet(sql, paramsList.ToArray());
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow row in ds.Tables[0].Rows)
                {
                    list.Add(ConvertToObject(Columns,row));
                }
            }
            return list;
        }

        internal void UpdateProperty(MetaDataProperty metaDataProperty)
        {
            List<IDbDataParameter> paramsList = new List<IDbDataParameter>() {
                this.DB.MakeInParam("@Description", TDbDataType.VarChar, metaDataProperty.Description),
                this.DB.MakeInParam("@HelperLink", TDbDataType.VarChar, metaDataProperty.HelperLink),
                this.DB.MakeInParam("@DemoCode", TDbDataType.VarChar, metaDataProperty.DemoCode),
                this.DB.MakeInParam("@LastModifier", TDbDataType.VarChar, metaDataProperty.LastModifier),
                this.DB.MakeInParam("@LastModifyTime", TDbDataType.DateTime, metaDataProperty.LastModifyTime),
                this.DB.MakeInParam("@ID", TDbDataType.VarChar, metaDataProperty.ID)
            };
            string sql = string.Format("Update {0} Set Description=@Description,HelperLink=@HelperLink,DemoCode=@DemoCode,LastModifier=@LastModifier,LastModifyTime=@LastModifyTime where ID=@ID", TableName);
            this.DB.ExecSqlStatement(sql, paramsList.ToArray());
        }
    }
}
